package com.me.chat.service.impl.relation;

import com.me.chat.mapper.relation.SingleRelationReqMapper;
import com.me.chat.pojo.relation.SingleRelationReq;
import com.me.chat.pojo.relation.qo.SingleRelationReqQO;
import com.me.chat.service.relation.SingleRelationReqService;
import com.me.chat.service.relation.SingleRelationService;
import com.me.chat.util.SimpleUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Authhor: qiu
 * @Date: 2021/12/20 8:34 上午
 */

@Service
public class SingleRelationReqServiceImpl implements SingleRelationReqService {

    @Autowired
    SingleRelationReqMapper singleRelationReqMapper;

    @Autowired
    SingleRelationService singleRelationService;

    @Override
    public List<SingleRelationReq> getSingleRelationReqByTargetId(int current,int size) {
        SingleRelationReqQO singleRelationReqQO = new SingleRelationReqQO();
        String userId = SimpleUserInfo.getUserId();
        singleRelationReqQO.setTargetId(userId);
        singleRelationReqQO.setPageVal(current, size);
        List<SingleRelationReq> singleRelationReqByTargetId = singleRelationReqMapper.getSingleRelationReqByTargetId(singleRelationReqQO);
        return singleRelationReqByTargetId;
    }

    @Override
    public int getSingleRelationReqCountByTargetId(SingleRelationReqQO singleRelationReqQO) {
        int singleRelationReqCount = singleRelationReqMapper.getSingleRelationReqCountByTargetId(singleRelationReqQO);
        return singleRelationReqCount;
    }

    @Override
    public int addSingleRelationRequest(SingleRelationReqQO singleRelationReqQO) {

        //在添加请求前判断下对方是否已经对自己发起了请求
        //targetId作为senderId来查询
        String targetId = singleRelationReqQO.getTargetId();
        //当前登录者的id
        String userId = SimpleUserInfo.getUserId();
        //设置判断参数
        SingleRelationReqQO bothReqObj = new SingleRelationReqQO();
        bothReqObj.setSenderId(targetId);
        bothReqObj.setTargetId(userId);

        String bothReqExist = singleRelationReqMapper.getRelationReqIdByBothId(bothReqObj);

        if(bothReqExist != null) {
            //说明存在，则直接两人成为好友
            singleRelationService.addSingleRelation(targetId,userId);
            singleRelationService.addSingleRelation(userId,targetId);

            //联系人添加完之后，要删除请求 targetId 和 userId 放置的位置没关系，因为sql用的是 (and) or (and)
            this.clearSingleRelationRequest(targetId,userId);
            //这个return 需要返回实时通讯状态码，由前端发送 201或者202
            return 202;
        }

        //设置发送者id
        singleRelationReqQO.setSenderId(userId);
        singleRelationReqQO.setCreatedTime(LocalDateTime.now());
        singleRelationReqMapper.addSingleRelationRequest(singleRelationReqQO);
        return 201;
    }

    @Override
    public void clearSingleRelationRequest(String user1, String user2) {
        singleRelationReqMapper.clearSingleRelationRequest(user1,user2);
    }
}
